#define _CRT_SECURE_NO_WARNINGS 1
#include <climits>
#include <iostream>
#include <vector>
using namespace std;
class Date {
    // friend ostream&operator<<(ostream&out,const Date&d);
public:
    int GetMonthDay(int year, int month);
    Date& operator+=(int day);
    Date(int year, int month, int day)
        :_year(year)
        , _month(month)
        , _day(day)
    {}


public:
    int _year;
    int _month;
    int _day;

};
int Date::GetMonthDay(int year, int month)
{
    vector<int> arr = { -1,31,28,31,30,31,30,31,31,30,31,30,31 };
    if (year % 400 == 0 || (year % 4 == 0) && (year % 100 != 0))
    {
        arr[2] = 29;
    }
    return arr[month];
}
Date& Date::operator+=(int day)
{


    _day += day;
    while (_day > GetMonthDay(_year, _month))
    {
        _day -= GetMonthDay(_year, _month);
        ++_month;
        if (_month == 13)
        {
            _year++;
            _month = 1;
        }
    }

    return *this;
}
//ostream&operator<<(ostream&out,const Date&d)
//{
//    out<<d._year<<d.
//}
int main() {
    int m;
    cin >> m;
    int year, month, day, num = 0;
    while (m > 0) {
        cin >> year >> month >> day >> num;
        Date d1(year, month, day);
        d1 += num;
        printf("%04d-%02d-%02d\n", d1._year, d1._month, d1._day);
        m--;
    }

}